package org.lex.expression.ui;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

public class TreePane extends JPanel {
	private JScrollPane scrollPane = null;
	private JTree tree = null;
	private DefaultTreeModel treeModel = null;
	private JLabel titleLabel = null;

	public TreePane() {
		super();
		initialize();
	}

	public void setRootNode(TreeNode root) {
		this.getTreeModel().setRoot(root);
	}

	private void initialize() {
		titleLabel = new JLabel();
		titleLabel.setText("Tree");
		this.setLayout(new BorderLayout());
		this.add(getScrollPane(), BorderLayout.CENTER);
		this.add(titleLabel, BorderLayout.NORTH);
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTree());
		}
		return scrollPane;
	}

	public JTree getTree() {
		if (tree == null) {
			tree = new JTree();
			tree.setModel(this.getTreeModel());
		}
		return tree;
	}

	private DefaultTreeModel getTreeModel() {
		if (null == this.treeModel) {
			this.treeModel = new DefaultTreeModel(null);
		}
		return treeModel;
	}
}
